<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>多态</title>
</head>

<body>
    <script>
        // -----------------01
        var Duck = function () { }
        var Chicken = function () { }

        var makeSound = function (animal) {
            if (animal instanceof Duck) {
                console.log("duck")
            } else if (animal instanceof Chicken) {
                console.log('chiken');
            }
        }

        makeSound(new Duck())
        makeSound(new Chicken())
        // 以上代码实现了简单的“多态性”，
        //但是如果要添加一只dog，那么就要修改makeSound函数
        //多态的核心思想：做什么，怎么做？

        // ----抽取
        var makeSound = function (animal) {
            animal.sound();
        }
        // ----封装
        var Duck = function () { }

        Duck.prototype.sound = function () {
            console.log('this is a duck');
        }
        var Chicken = function () { }
        Chicken.prototype.sound = function () {console.log('this is  a checken'); }
        makeSound()

    </script>
</body>

</html>